<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {
	
	/*登录功能*/
    public function index(){
    	if(IS_POST){
    		$username=I('post.username');
			$password=I('post.password');
			$code=I('post.verify',null,'strtoupper,htmlspecialchars');
			
			//验证验证码
			$Verify =new \Think\Verify();
			if(!$Verify->check($code)) $this->error('验证码不正确！');
			
			//判断登录信息是否正确
			$user=M('admin')->where(array('username'=>$username))->find();
			if(!$user) $this->error('用户名不存在！');
			if($user['password']!=md5($password)) $this->error('用户名或者密码错误！');
			if($user['lock']==1) $this->error('用户已经被锁定，请联系管理员！');
			
			//获取ip登录时间
			$ip=$user['loginip']?$user['loginip']:'第一次登录';
			$time=$user['logintime']?$user['logintime']:'无';
			
			//设置cookie过期时间  session
			cookie(session_name(),session_id(),C('COOKIE_EXPIRE'));
			session('username',$username);
			session('adid',$user['adid']);
			session('loginip',$ip);
			session('logintime',$time);
			
			/*写入数据库*/
			$loginInfo=array();
			$loginInfo['loginip']=get_client_ip(0);
			$loginInfo['logintime']=time();
			M('admin')->where(array('username'=>$username))->save($loginInfo);
			
			$this->success('登录成功，正在跳转！',__APP__ .'/Admin/index');
			
    	}else{
    		$this->display('login');
    	}
    	
    }
	/*显示验证码*/
	public function verify(){
		$config =    array(
		    'fontSize'    =>    13,    // 验证码字体大小
		    'length'      =>    4,     // 验证码位数
		    'useNoise'    =>    false, // 关闭验证码杂点
		    'imageW'	  =>    100,
		    'imageH'	  =>	30	
		);
		$Verify =new \Think\Verify($config);
		$Verify->entry();
	}
	/*退出*/
	public function out(){
		session_unset();
		session_destroy();
		$this->success('退出成功！');
	}
}